﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TimingFileCopy
{
    public partial class Form1 : Form
    {
        private bool isCic = false;

        public Form1()
        {
            InitializeComponent();
            Task.Factory.StartNew(() =>
            {
                DateTime createTime = DateTime.MinValue;
                while (true)
                {
                    if (isCic)
                    {
                        List<String> FileList = FileHelper.GetAllPureSubDirs(lab_destination.Text);
                        foreach (var item in FileList)
                        {
                            FileInfo fi = new FileInfo(lab_destination.Text + "\\" + item);
                            if (fi.CreationTime > createTime)
                            {
                                createTime = fi.CreationTime;
                                if (lab_LastCopyTime.InvokeRequired)
                                {
                                    this.lab_LastCopyTime.Invoke(new Action<string>(s => this.lab_LastCopyTime.Text = s), DateTime.Now.ToString());//委托类的基类
                                    this.lab_descFileCount.Invoke(new Action<string>(s => this.lab_descFileCount.Text = s), "存在：" + FileList.Count);//委托类的基类
                                }
                                lab_LastCopyTime.Text = createTime.ToString();
                            }
                        }
                        if (createTime.Subtract(DateTime.Now).Duration().Days >= 7)
                        {
                            DirectoryInfo source = new DirectoryInfo(lab_SourceFile.Text);
                            FileHelper.CopyDirectory(lab_SourceFile.Text, lab_destination.Text + $"\\{DateTime.Now.ToString("yyyy-MM-dd")}\\" + source.Name);
                        }
                    }
                    Task.Delay(10000);
                }
            });
        }

        private void btn_startCopy_Click(object sender, EventArgs e)
        {
            isCic = true;
        }

        private void btn_Stop_Click(object sender, EventArgs e)
        {
            isCic = false;
        }

        private void btn_sourceButton_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "请选择源文件夹";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }

                lab_SourceFile.Text = dialog.SelectedPath;
            }
        }

        private void btn_destination_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "请选择目标文件夹";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }

                lab_destination.Text = dialog.SelectedPath;
            }
        }
    }
}